/*
 * Copyright 2007-2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.paoding.rose.web.instruction;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import net.paoding.rose.web.Invocation;

import org.apache.commons.lang.StringUtils;

/**
 * 
 * @author zhiliang.wang [qieqie.wang@paoding.net]
 * 
 */
public class TextInstruction extends AbstractInstruction {

	@Override
	public void doRender(Invocation invocation)	throws Exception {
		String encoding = resolvePlaceHolder(this.encoding, invocation);
		String contentType = resolvePlaceHolder(this.contentType, invocation);
		String text = resolvePlaceHolder(this.text, invocation);
		
		HttpServletResponse response = invocation.getResponse();
		if (contentType != null) {
			response.setContentType(contentType);
		}
		if (encoding != null) {
			response.setCharacterEncoding(encoding);
		}
		if (StringUtils.isNotEmpty(text)) {
			PrintWriter out = response.getWriter();
			out.write(text);
			out.flush();
		}
	}

	//-------------------------------------------
	
	private String text;
	private String contentType;
	private String encoding;

	public TextInstruction contentType(String contentType) {
		this.contentType = contentType;
		return this;
	}

	public String contentType() {
		return contentType;
	}

	public TextInstruction encoding(String encoding) {
		this.encoding = encoding;
		return this;
	}

	public String encoding() {
		return encoding;
	}

	public String text() {
		return text;
	}

	public TextInstruction plain(String text) {
		this.text = text;
		return this;
	}

	public TextInstruction html(String html) {
		this.text = html;
		contentType("text/html");
		return this;
	}

	public TextInstruction xml(String xml) {
		this.text = xml;
		contentType("text/xml");
		return this;
	}

	public TextInstruction json(String json) {
		this.text = json;
		contentType("text/json");
		return this;
	}

	@Override
	public String toString() {
		return text;
	}
}
